package com.krain.meijusky.activity;

import android.content.Intent;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.krain.corelibrary.widget.RefreshRecyclerView;
import com.krain.corelibrary.widget.loadingView.LoadingView;
import com.krain.corelibrary.widget.recyleview.implement.OnRecyclerItemListener;
import com.krain.meijusky.R;
import com.krain.meijusky.adapter.SearchResultAdapter;
import com.krain.meijusky.entity.ResourceEntity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 亚敏 on 2015/11/18.
 */
@EActivity(R.layout.activity_searchresult)
public class SearchResultActivity extends BaseActivity {

    private final String ROOTURL = "http://www.meijutt.com/search.asp";
    private final String SEARCH_KEY = "searchword";
    private List<ResourceEntity> mEntityList;

    @ViewById(R.id.loadView)
     LoadingView mLoadingView;


    @ViewById(R.id.act_search_result_recyleview)
    RefreshRecyclerView mRefreshRecyclerView;

    @Extra("keyword")
    String keyWord;//搜索关键字

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.search_result);
    }

    @AfterViews
    void afterViews() {
        mRefreshRecyclerView.showRefreshing(false);
        mRefreshRecyclerView.setIsLoadMore(false);
        getSearchResult(mEntityList,mRefreshRecyclerView,mLoadingView);
    }

    @Background
    protected void getSearchResult(List<ResourceEntity> mEntityList,RefreshRecyclerView mRefreshRecyclerView,LoadingView loadingView) {
        try {
            Document document = Jsoup.connect(ROOTURL).data(SEARCH_KEY, keyWord).postDataCharset("GBK").post();
            Elements listElements = document.select("div[class=bor_img3_right]");
            Elements listAttribute = document.select("ul[class=list_20]");
            mEntityList = new ArrayList<>();
            getListEntity(listElements, listAttribute, mEntityList,mRefreshRecyclerView,loadingView);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置实体列表值
     *
     * @param contents
     * @param attributes
     */
    void getListEntity(Elements contents, Elements attributes, List<ResourceEntity> mEntityList,RefreshRecyclerView mRefreshRecyclerView
            ,LoadingView loadingView) {
        for (int i = 0; i < contents.size(); i++) {
            Elements keys = attributes.get(i).select("font[class=cor_move_li2]");
            Elements values = attributes.get(i).select("font[class=cor_move_li]");
            values.add(values.size() - 1, attributes.get(i).select("font[color]").first() == null ? attributes.get(i).select("span[style]").first() : attributes.get(i).select("font[color]").first());
            ResourceEntity entity = new ResourceEntity();
            Map<String, String> map = new ArrayMap<>();
            for (int j = 0; j < keys.size(); j++) {
                String key = keys.get(j).text();
                String value = values.get(j).text();
                map.put(key, value);
            }
            entity.setAttributes(map);
            //设置跳转链接标题等内容
            Element tag_img = contents.get(i).select("img").first();
            Element tag_a = contents.get(i).select("a").first();
            entity.setNextUrl(tag_a.attr("href"));
            entity.setTitle(tag_img.attr("alt"));
            entity.setImg(tag_img.attr("data-original"));
            mEntityList.add(entity);
        }
        setAdapter(mEntityList,mRefreshRecyclerView,loadingView);
    }

    @UiThread
    protected void setAdapter(List<ResourceEntity> mEntityList,RefreshRecyclerView mRefreshRecyclerView,LoadingView loadingView) {
        SearchResultAdapter adapter = new SearchResultAdapter(mEntityList, mContext);
        mRefreshRecyclerView.setAdapter(adapter);
        adapter.setOnItemListener(new MyRecyclerItemListener(mEntityList));
        loadingView.showContentView(mRefreshRecyclerView);
    }


    /**
     * 列表的点击事件
     */
    class MyRecyclerItemListener extends OnRecyclerItemListener{
        List<ResourceEntity> mEntityList;

        public MyRecyclerItemListener(List<ResourceEntity> mEntityList) {
            this.mEntityList = mEntityList;
        }

        @Override
        public void onItemClick(int position, int viewType, RecyclerView.ViewHolder holder, View v) {
            DetailActivity.jumpTothisActivity(mContext, mEntityList.get(position), ((SearchResultAdapter.ViewHolder) holder).item_sr_img);
        }
    }

    public static void jumpTothisActivity(BaseActivity activity, String keyWord) {
        Intent intent = new Intent(activity, SearchResultActivity_.class);
        intent.putExtra("keyword", keyWord);
        activity.startActivity(intent);
    }


}
